class_name TurnCounter
extends Resource

# 回合计数器 - 追踪回合内的状态变化
# 用于支持基于条件变化的卡牌效果

signal counter_changed(counter_name: String, old_value: int, new_value: int)

# 计数器存储
var counters: Dictionary = {}
var turn_start_values: Dictionary = {}

# 计数器类型枚举
enum CounterType {
	HEALTH_LOST,        # 失去的血量
	HEALTH_GAINED,      # 获得的血量  
	BLOCK_GAINED,       # 获得的护盾
	BLOCK_LOST,         # 失去的护盾
	MANA_SPENT,         # 消耗的能量
	MANA_GAINED,        # 获得的能量
	MANA_OVERSPENT,     # 过量消耗的能量（本回合花费超过当前能量的部分）
	DAMAGE_DEALT,       # 造成的伤害
	DAMAGE_TAKEN,       # 受到的伤害
	CARDS_PLAYED,       # 使用的卡牌数
	CARDS_DRAWN,        # 抽取的卡牌数
	CARDS_DISCARDED,    # 丢弃的卡牌数
	CUSTOM_1,           # 自定义计数器1
	CUSTOM_2,           # 自定义计数器2
	CUSTOM_3            # 自定义计数器3
}

func _init():
	reset_all_counters()

# 重置所有计数器（回合开始时调用）
func reset_all_counters() -> void:
	counters.clear()
	turn_start_values.clear()
	
	# 初始化所有计数器为0
	for counter_type in CounterType.values():
		var counter_name = CounterType.keys()[counter_type]
		counters[counter_name] = 0
		turn_start_values[counter_name] = 0

# 设置回合开始时的基准值
func set_turn_start_value(counter_type: CounterType, value: int) -> void:
	var counter_name = CounterType.keys()[counter_type]
	turn_start_values[counter_name] = value

# 增加计数器值
func add_to_counter(counter_type: CounterType, amount: int) -> void:
	var counter_name = CounterType.keys()[counter_type]
	var old_value = counters.get(counter_name, 0)
	var new_value = old_value + amount
	counters[counter_name] = new_value
	
	print("📊 计数器变化: %s %d → %d (+%d)" % [counter_name, old_value, new_value, amount])
	counter_changed.emit(counter_name, old_value, new_value)

# 设置计数器值
func set_counter(counter_type: CounterType, value: int) -> void:
	var counter_name = CounterType.keys()[counter_type]
	var old_value = counters.get(counter_name, 0)
	counters[counter_name] = value
	
	print("📊 计数器设置: %s %d → %d" % [counter_name, old_value, value])
	counter_changed.emit(counter_name, old_value, value)

# 获取计数器值
func get_counter(counter_type: CounterType) -> int:
	var counter_name = CounterType.keys()[counter_type]
	return counters.get(counter_name, 0)

# 获取计数器变化量（相对于回合开始）
func get_counter_change(counter_type: CounterType) -> int:
	var counter_name = CounterType.keys()[counter_type]
	var current_value = counters.get(counter_name, 0)
	var start_value = turn_start_values.get(counter_name, 0)
	return current_value - start_value

# 检查条件是否满足
func check_condition(counter_type: CounterType, condition_type: String, threshold: int) -> bool:
	var current_value = get_counter(counter_type)
	var change_value = get_counter_change(counter_type)
	
	match condition_type:
		"equals":
			return current_value == threshold
		"greater_than":
			return current_value > threshold
		"less_than":
			return current_value < threshold
		"at_least":
			return current_value >= threshold
		"at_most":
			return current_value <= threshold
		"changed_by":
			return change_value == threshold
		"changed_by_at_least":
			return change_value >= threshold
		"changed_by_at_most":
			return change_value <= threshold
		_:
			print("❌ 未知条件类型: %s" % condition_type)
			return false

# 获取所有计数器信息（调试用）
func get_all_counters_info() -> Dictionary:
	var info = {}
	for counter_name in counters.keys():
		info[counter_name] = {
			"current": counters[counter_name],
			"start": turn_start_values[counter_name],
			"change": counters[counter_name] - turn_start_values[counter_name]
		}
	return info

# 打印所有计数器状态（调试用）
func print_all_counters() -> void:
	print("📊 === 计数器状态 ===")
	for counter_name in counters.keys():
		var current = counters[counter_name]
		var start = turn_start_values[counter_name]
		var change = current - start
		if current > 0 or change != 0:
			print("  %s: %d (变化: %+d)" % [counter_name, current, change])
	print("📊 ==================") 